
package com.skcc.cop.mrtaxi4driver;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;

import com.skcc.cop.mrtaxi4driver.http.BizGateway;

/**
 * 
 * @author project team
 */ 
public abstract class BaseActivity extends FragmentActivity {
    
	// ===========================================================
    // Constants
    // ===========================================================
    private static final String TAG = BaseActivity.class.getSimpleName();
    
    protected MrTaxi4DriverApplication application;
    protected ProgressDialog progressDialog;
    
    protected BizGateway gateway;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, " in onCreate >>>>>");
        
        application = (MrTaxi4DriverApplication) this.getApplication();
        
        if (gateway == null) {
        	gateway = new BizGateway(this, MrTaxi4DriverApplication.SERVER_URL);
        }
    }

	@Override
    protected void onResume()
    {
        Log.d(TAG, " in onResume >>>>>");
        
        super.onResume();
    }
    
    @Override
    protected void onPause()
    {
        Log.d(TAG, " in onPause >>>>>");
        
        if (progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.dismiss();
            progressDialog = null;
        }
        super.onPause();
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        
        Log.d(TAG, " in onDestroy >>>>>");
    }
    
    /**
     * show a progress dialog if it is not showing.
     * 
     * @param resId
     */
    public void showProgressDialog(Context context, int loading)
    {
    	if (progressDialog == null || !progressDialog.isShowing())
        {
            try
            {
                progressDialog = ProgressDialog.show(context, getString(R.string.app_name), getString(loading), true);
                progressDialog.setContentView(R.layout.dialog_progress);
            } catch (Exception e)
            {
                // ignore this exception. maybe
                // android.view.WindowManager$BadTokenException will occurs when
                // activity is finishing.
            }
        }
    }
    
    /**
     * show a progress dialog if it is not showing.
     * 
     * @param resId
     */
    public void showProgressDialog(Context context)
    {
        if (progressDialog == null || !progressDialog.isShowing())
        {
            try
            {
                progressDialog = ProgressDialog.show(context, null, getString(R.string.loading), false, false);
            } catch (Exception e)
            {
                // ignore this exception. maybe
                // android.view.WindowManager$BadTokenException will occurs when
                // activity is finishing.
            }
        }
    }
    
    protected void setProgressDialogMessage(String message)
    {
        if (progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.setMessage(message);
        }
    }
    
    /**
     * dismiss a progress dialog if it is showing.
     */
    public void dismissProgressDialog()
    {
        if (progressDialog != null && progressDialog.isShowing())
        {
            progressDialog.dismiss();
        }
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent event)
    {
        return super.dispatchTouchEvent(event);
    }
    
//    public LayoutInflater getInflater()
//    {
//        return application.getInflater();
//    }

}
